package com.ztesoft.huangfu.learning.springboot.springbootaop.validator.constraintvalidators;





import com.ztesoft.huangfu.learning.springboot.springbootaop.validator.constraints.StringEnum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import static org.apache.commons.lang.ArrayUtils.contains;

/**
 * Created by ZaoSheng on 15-1-26.
 */
public class StringEnumValidate implements ConstraintValidator<StringEnum, String> {
    private String[] type;

    private boolean allowNull = false;

    @Override
    public void initialize(StringEnum stringEnumValidate) {
        this.type = stringEnumValidate.type();
        this.allowNull = stringEnumValidate.allowNull();
    }

    @Override
    public boolean isValid(String string, ConstraintValidatorContext constraintValidatorContext) {
        if (type == null || type.length == 0 || (string == null && allowNull)) {
            return true;
        }
        return contains(type, string);
    }


}
